# $Id$


include Makefile.in


all: $(stage2_package) update.rdf

stage1: $(stage1_package)

stage2: $(stage2_package)

clean:
	rm -rf "$(stage1_build_dir)" "$(stage2_build_dir)" "../$(l10n_dir)/mi"
	rm -f *.tmp.png update.rdf "$(stage1_package)"

distclean: clean
	rm -f "$(stage2_package)"

test:
	cd $(base_dir); \
	for file in $$(find "$(l10n_dir)/mi.$(build)" -name '*.dtd' -type f | xargs); do \
		xmllint --nonet --dtdvalid file://$(base_dir)/$$file "$(customization_dir)/test.xml"; \
	done


update.rdf: $(stage2_package)
	sha384sum=$$( sha384sum -b "$<" | awk '{ print $$1 }' ); \
		sed "update.beta.rdf" \
			-e 's|TAG_PACKAGE_GUID|$(package_guid)|g' \
			-e 's|TAG_PACKAGE_VERSION|$(package_version)|g' \
			-e 's|TAG_PROJECT_MAX_VERSION|$(project_max_version)|g' \
			-e 's|TAG_PROJECT_MIN_VERSION|$(project_min_version)|g' \
			-e 's|TAG_PACKAGE_BETA_FILE_URL|$(package_beta_file_url)|g' \
			-e "s|TAG_SHA384SUM|$$sha384sum|g" \
			> "update.rdf";

$(stage1_package):
	rm -rf "$(stage1_build_dir)" "../$(l10n_dir)/mi"
	mkdir "$(stage1_build_dir)"
	cd "../$(l10n_dir)"; \
	cp -rp "mi.$(build)" "mi"; \
	cp -rp mi.override/* "mi/"
	cd "$(stage1_build_dir)"; \
	../mozilla/configure --disable-compile-environment --enable-application=browser
#	../mozilla/configure --enable-application=browser --with-windows-version=501 --enable-ui-locale=mi
	make -C "$(stage1_build_dir)/browser/locales" langpack-mi
	cp "$(stage1_package_file)" $(stage1_package)


$(stage2_package): $(stage1_package)
	rm -rf "$(stage2_build_dir)"
	mkdir "$(stage2_build_dir)"
	cd "$(stage2_build_dir)"; \
	unzip "$(stage1_package_file)"
	perl -p -i \
		-e 's|em:version=".*"|em:version="$(package_version)"|' \
		"$(stage2_build_dir)/install.rdf"
	perl -p -i \
		-e 's|    <em:targetApplication>|    <em:description>$(package_150c_description)</em:description>\n    <em:homepageURL>$(project_homepage)</em:homepageURL>\n    <em:iconURL>chrome://mi/content/icon.png</em:iconURL>\n    <em:updateKey>MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCz/vQ/8C62NtxHn6Dtu8gxFIZM/ECcFuJyspkyATrMtnKIFndOgM49K/xpWzOCWpVr5HhbmoVUWTXC8rDbWQs1pDmIfRzRX2RyOPxMw0Aw6h5eeWFiHZ0c3YDFe50aUmM5L3ChXeTkguZ1VSp2GopnZim4n91Uj0pYx1TyuKPhsQIDAQAB</em:updateKey>\n    <em:updateURL>$(package_beta_update_url)</em:updateURL>\n\n    <em:targetApplication>|' \
		"$(stage2_build_dir)/install.rdf"
	perl -p -i \
		-e 's|<em:minVersion>.*</em:minVersion>|<em:minVersion>$(project_min_version)</em:minVersion>|' \
		"$(stage2_build_dir)/install.rdf"
	perl -p -i \
		-e 's|<em:maxVersion>.*</em:maxVersion>|<em:maxVersion>$(project_max_version)</em:maxVersion>|' \
		"$(stage2_build_dir)/install.rdf"
	perl -p -i \
		-e 's|em:name=".*"|em:name="$(package_name)"|' \
		"$(stage2_build_dir)/install.rdf"
	echo "content mi chrome/chrome" >> "$(stage2_build_dir)/chrome.manifest"
	gm convert -size 32x32 xc:transparent icon.1.tmp.png
	gm convert -resize 32x32 $(project_icon) icon.2.tmp.png
	gm composite -gravity center "icon.2.tmp.png" "icon.1.tmp.png" "$(stage2_build_dir)/chrome/icon.png"
	cd "$(stage2_build_dir)"; \
	zip -r9 "../$(stage2_package_file)" *
